package com.gopay.scheduler.job;

import com.gopay.common.constants.trans.AccountingPoint;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.remote.cps.bis.Trans02302RollBackAccountRemoteService;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.OrderLifecycleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lij-jiang
 * @ClassName: MicroPayOederManagerJob.java
 * @Description: 小额免密支付撤销处理调用bis失败的一阶段账务回滚
 * @date 2017年06月26日 10:37
 */
public class MicroPayOederManagerJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(MicroPayOederManagerJob.class);

    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;
    private MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient;

    public static final String TRANSTYPE = "02302";
    private static final int QUERYCOUNT = 5000;

    @Resource(name = "cpsGeneralMainOrderOperClient")
    public void setCpsGeneralMainOrderOperClient(MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient) {
        this.cpsGeneralMainOrderOperClient = cpsGeneralMainOrderOperClient;
    }

    @Resource(name = "cpsGeneralMainOrderDAO")
    public void setCpsGeneralMainOrderDAO(CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO) {
        this.cpsGeneralMainOrderDAO = cpsGeneralMainOrderDAO;
    }

    @Resource(name = "trans02302RollBackAccountRemoteService")
    private Trans02302RollBackAccountRemoteService trans02302RollBackAccount;
    @Override
    protected void doJobInternal() {
        logger.error("----小额免密支付撤销 一阶段账务回滚 start ----");
        long startMillions = 0;
        try {
            Map param = new HashMap();
            String b2cName = TRANSTYPE + OrderLifecycleUtil.B2C;
            String b2c = OrderLifecycleUtil.getLifecycle(b2cName);
            param.put("b2c", b2c);
            // 每次1000条
            startMillions = System.currentTimeMillis();
            logger.info("查询超时订单执行开始 ");
            List<Object> orderList = findTimeoutOrdersIntegrationPay(param, 1, QUERYCOUNT);
            if(!orderList.isEmpty()) {
                for (int i = 0; i < orderList.size(); i++) {
                    String orderId = String.valueOf(orderList.get(i));
                    trans02302RollBackAccount.rollBackAccount(orderId);
                }
            }
        } catch (Exception e) {
            logger.error("小额免密支付撤销 一阶段账务回滚订单处理异常信息", e.getMessage());
        }
        long endMiilions = System.currentTimeMillis();
        long result = (endMiilions - startMillions) / 1000;
        logger.info("超时订单执行结束 耗费=" + result + "秒");

        logger.error("----小额免密支付撤销 一阶段账务回滚 end ----");
    }


    /**
     * 聚合支付&小额免密超时订单
     */
    private List<Object> findTimeoutOrdersIntegrationPay(Map param, int pageNo, int pageSize) {
        StringBuffer sb = new StringBuffer("select gopay_order_id from CPS_GEN_MAIN_ORDER ");
        sb.append(" where ACCOUNTING_POINT =:accountPoint and TXN_STA_CD ='30101' ");
        sb.append(" and GOPAY_INT_TXN_CD = '02302' ");
        sb.append(" and TRUNC(TO_NUMBER(SYSDATE - (CREATED_TS+0))*24*60)>=:timeSpan ");
        Map m = new HashMap();
        m.put("timeSpan",
                String.valueOf(param.get("b2c")).replaceAll("[a-zA-Z]+", ""));
        m.put("accountPoint", AccountingPoint._02302_DEFAULT.value);
        List<?> result = cpsGeneralMainOrderDAO.findBySql(sb.toString(), m, pageNo, pageSize);
        return (List<Object>) result;
    }


}
